package com.zheng.study.base.service.impl;

import com.zheng.study.base.bean.BaseEnumManager;
import com.zheng.study.base.bean.search.Search;
import com.zheng.study.base.bean.search.order.Order;
import com.zheng.study.base.bean.search.page.PageHelp;
import com.zheng.study.base.bean.search.page.PagePaging;
import com.zheng.study.base.bean.search.result.*;
import com.zheng.study.base.bean.search.rule.Rule;
import com.zheng.study.base.dao.BaseDao;
import com.zheng.study.base.entity.BaseEntity;
import com.zheng.study.base.entity.NameEntity;
import com.zheng.study.base.service.BaseService;
import com.zheng.study.base.service.common.ResultService;
import com.zheng.study.base.utils.PinyinUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2018/8/16 。
 * ============================================================================
 */
@Service
public abstract class BaseServiceImpl<T extends BaseEntity, PK extends Serializable> implements BaseService<T, PK> {

	public abstract BaseDao<T, PK> getBaseDao();

	@Resource
	protected ResultService resultService;

	@Override
	public T load(PK id) {
		return getBaseDao().load(id);
	}

	@Override
	public T get(PK id) {
		return getBaseDao().get(id);
	}

	@Override
	public PK save(T entity) {
		if(entity instanceof NameEntity){
			if(((NameEntity)entity).getState() == null){
				((NameEntity)entity).setState(BaseEnumManager.StateEnum.Enable);
			}
			if(StringUtils.isNotEmpty(((NameEntity)entity).getName())){
				((NameEntity)entity).setPinYin(PinyinUtil.getPingYin(((NameEntity)entity).getName()));
				((NameEntity)entity).setPinYinHead(PinyinUtil.getPinYinHeadChar(((NameEntity)entity).getName()));
			}
		}
		return getBaseDao().save(entity);
	}

	@Override
	public void delete(T entity) {
		getBaseDao().delete(entity);
	}

	@Override
	public void delete(PK id) {
		getBaseDao().delete(id);
	}

	@Override
	public void update(T entity) {
		if (entity instanceof NameEntity) {
			if (StringUtils.isNotEmpty(((NameEntity) entity).getName())) {
				((NameEntity)entity).setPinYin(PinyinUtil.getPingYin(((NameEntity)entity).getName()));
				((NameEntity)entity).setPinYinHead(PinyinUtil.getPinYinHeadChar(((NameEntity)entity).getName()));
			}
		}
		getBaseDao().update(entity);
	}

	@Override
	public List<T> get(PK[] ids) {
		return null;
	}

	@Override
	public T get(String propertyName, Object value) {
		return getBaseDao().get(propertyName, value);
	}

	@Override
	public boolean isUnique(String propertyName, Object oldValue, Object newValue) {
		return getBaseDao().isUnique(propertyName, oldValue, newValue);
	}

	@Override
	public boolean isExist(String propertyName, Object value) {
		return getBaseDao().isExist(propertyName, value);
	}

	@Override
	public List<T> getList(String propertyName, Object value) {
		return getBaseDao().getList(propertyName, value);
	}

	@Override
	public List<T> getAll() {
		return getBaseDao().getAll();
	}

	@Override
	public Long getTotalCount() {
		return getBaseDao().getTotalCount();
	}

	@Override
	public void flush() {
		getBaseDao().flush();
	}

	@Override
	public void clear() {
		getBaseDao().clear();
	}

	@Override
	public void evict(Object object) {
		getBaseDao().evict(object);
	}

	@Override
	public Query createQuery(String hql) {
		return getBaseDao().createQuery(hql);
	}

	@Override
	public SQLQuery createSqlQuery(String sql) {
		return getBaseDao().createSqlQuery(sql);
	}

	@Override
	public Object getUniqueResult(String hql) {
		return getBaseDao().getUniqueResult(hql);
	}

	@Override
	public List<T> getList(String hql) {
		return getBaseDao().getList(hql);
	}

	@Override
	public Object getUniqueResult(String hql, Map<String, Object> params) {
		return getBaseDao().getUniqueResult(hql, params);
	}

	@Override
	public List<T> getList(String hql, Map<String, Object> params) {
		return getBaseDao().getList(hql, params);
	}

	@Override
	public List<Object[]> getObjectList(String hql, Map<String, Object> params) {
		return getBaseDao().getObjectList(hql, params);
	}

	@Override
	public List<Map<String, Object>> getMapList(String hql, Map<String, Object> params) {
		return getBaseDao().getMapList(hql, params);
	}

	@Override
	public SearchResult findBySearch(Search search) {
		return getBaseDao().findBySearch(search);
	}

	@Override
	public Object getObjectEntity(String keyId, String className) {
		return getBaseDao().getObjectEntity(keyId, className);
	}

	@Override
	public SearchResultCount findCountBySearch(List<Rule> rules) {
		return (SearchResultCount) findBySearch(Search.newBuilder().setPage(PageHelp.rowCount()).addRules(rules).build());
	}

	@Override
	public SearchResultUnique findUniqueBySearch(List<Rule> rules) {
		return (SearchResultUnique) findBySearch(Search.newBuilder().setPage(PageHelp.rowCount()).addRules(rules).build());
	}

	@Override
	public SearchResultList findListBySearch(List<Rule> rules, Order... orders) {
		if(orders == null || orders.length == 0){
			orders = new Order[]{Order.simpleOrder()};
		}
		return (SearchResultList) findBySearch(Search.newBuilder().setPage(PageHelp.list()).addRules(rules).addOrders(orders).build());
	}

	@Override
	public SearchResultPaging findPagingBySearch(PagePaging page, List<Rule> rules, Order... orders) {
		if(page == null){
			page = PagePaging.page(0, 0); //表示没有分页，查询所有的
		}

		if(orders==null || orders.length==0){
			orders = new Order[]{Order.simpleOrder()};
		}

		return (SearchResultPaging) findBySearch(Search.newBuilder().setPage(page).addRules(rules).addOrders(orders).build());
	}
}
